// StiFlossTable.cpp: implementation of the CStiFlossTable class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CrossStitch.h"
#include "StiFlossTable.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CStiFlossTable::CStiFlossTable()
{

}

CStiFlossTable::~CStiFlossTable()
{
	Clear();
}

FLOSS* CStiFlossTable::GetFloss( int id )
{
	POSITION pos = m_Flosses.FindIndex(id);
	return m_Flosses.GetAt(pos);
}

void CStiFlossTable::AddFloss( FLOSS* floss )
{
	m_Flosses.AddTail(floss);	
}

void CStiFlossTable::RemoveFloss( FLOSS* floss )
{
	POSITION pos = m_Flosses.Find(floss);
	if (pos)
	{
		m_Flosses.RemoveAt(pos);
	}
}

void CStiFlossTable::ChangeFloss( FLOSS* floss )
{
	POSITION pos = m_Flosses.Find(floss);
	if (pos)
	{
		memcpy(m_Flosses.GetAt(pos), floss, sizeof FLOSS);
	}

}

void CStiFlossTable::Serialize( CArchive& ar )
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar << m_Flosses.GetCount();
		POSITION pos = m_Flosses.GetHeadPosition();
		while (pos)
		{
			FLOSS* floss = m_Flosses.GetNext(pos);
			ar.Write(floss, sizeof FLOSS);
		}
	}
	else
	{
		// TODO: add loading code here
		int count = 0;
		ar >> count;
		while (count--)
		{
			FLOSS* floss = new FLOSS;
			ar.Read(floss, sizeof FLOSS);
			m_Flosses.AddTail(floss);
		}
	}

}

void CStiFlossTable::DrawFlossTable( CDC* pDC,int sx, int sy, int line)
{
	int dx, dy;
	dx = FLOSS_POINT;
	dy = FLOSS_POINT;
	int i=0;
	
	POSITION pos = m_Flosses.GetHeadPosition();
	while (pos)
	{
		FLOSS* floss = m_Flosses.GetNext(pos);
		pDC->FillSolidRect(sx+(i%line)*FLOSS_INN, sy+i/line*FLOSS_INN, dx, dy, floss->color);
		pDC->Draw3dRect(sx+(i%line)*FLOSS_INN, sy+i/line*FLOSS_INN, dx, dy, RGB(0,0,0), RGB(0,0,0));
		i++;
	}
	
}

void CStiFlossTable::SetFlosses( CList<FLOSS*, FLOSS*>* pFlosses )
{
	POSITION pos = pFlosses->GetHeadPosition();
	while (pos)
	{
		FLOSS* floss = pFlosses->GetNext(pos);
		AddFloss(floss);
	}
}

